/**
 * @author l.lin
 * @created 17/07/14 17:04
 */
(function(angular, backToTop) {
    'use strict';
    angular.module('myapp',['ngResource', 'datatables', 'ui.bootstrap']).controller('bootstrapIntegrationCtrl', function ($scope, DTOptionsBuilder, DTColumnBuilder) {
        $scope.dtOptions = DTOptionsBuilder
            .fromSource('/?a=allProjects')
            // Add Bootstrap compatibility
            .withBootstrap()
            .withBootstrapOptions({
                TableTools: {
                    classes: {
                        container: 'btn-group',
                        buttons: {
                            normal: 'btn btn-danger'
                        }
                    }
                },
                ColVis: {
                    classes: {
                        masterButton: 'btn btn-primary'
                    }
                }
            })

            // Add ColVis compatibility
            .withColVis()
            .withColVisStateChange(function(iColumn, bVisible) {
                bVisible?($('#th'+iColumn).show()):($('#th'+iColumn).hide());
                // $('#th'+iColumn).visible=bVisible;
            })
            // Add Table tools compatibility
            .withTableTools('/static/script/angular/vendor/datatables-tabletools/swf/copy_csv_xls_pdf.swf')
            .withTableToolsButtons([
                'copy',
                'print', {
                    'sExtends': 'collection',
                    'sButtonText': 'Save',
                    'aButtons': [
                        {'sExtends':'xls','sButtonText': "筛选后数据","oSelectorOpts": {filter: 'applied', order: 'current'}, "sCharSet": "utf16le"},
                        {'sExtends':'xls','sButtonText': "当前页","oSelectorOpts": {page: 'current'}, "sCharSet": "utf16le"},
                        {'sExtends':'xls','sButtonText': "全部数据", "sCharSet": "utf16le"},
                        {'sExtends':'pdf','sButtonText': "全部为PDF", "sCharSet": "utf8"}
                    ]
                    
                }
            ]);
        $scope.dtColumns = [
            DTColumnBuilder.newColumn('qlty_project_id').withTitle('项目ID'),
            DTColumnBuilder.newColumn('name').withTitle('项目名称'),
            DTColumnBuilder.newColumn('department_id').withTitle('学院'),
            DTColumnBuilder.newColumn('teacher_name').withTitle('主持人姓名'),
            DTColumnBuilder.newColumn('type').withTitle('项目类型'),
            DTColumnBuilder.newColumn('category').withTitle('项目类别'),
            DTColumnBuilder.newColumn('status').withTitle('状态'),
            DTColumnBuilder.newColumn('created_time').withTitle('创建时间'),
            DTColumnBuilder.newColumn('apply_time').withTitle('提交申报时间'),
            DTColumnBuilder.newColumn('established_time').withTitle('立项时间'),
            DTColumnBuilder.newColumn('change_time').withTitle('调整时间'),
            DTColumnBuilder.newColumn('middle_time').withTitle('中期时间'),
            DTColumnBuilder.newColumn('done_time').withTitle('结题时间'),
            DTColumnBuilder.newColumn('rank').withTitle('等级'),
            DTColumnBuilder.newColumn('belong_cqdd').withTitle('高职或电大'),
            DTColumnBuilder.newColumn('NO').withTitle('项目编号'),
            DTColumnBuilder.newColumn('apply_funds').withTitle('项目经费'),
            DTColumnBuilder.newColumn('professional_title').withTitle('职称'),
            DTColumnBuilder.newColumn('teacher_sex').withTitle('性别'),
            DTColumnBuilder.newColumn('email').withTitle('邮箱'),
            DTColumnBuilder.newColumn('mobile_phone').withTitle('手机')
        ];
        $scope.$on('event:dataTableLoaded', function(event, loadedDT) {
          // Setup - add a text input to each footer cell
          var id = '#' + loadedDT.id;
          var html ='';
          $(id + ' thead th').each(function() {
            var title = $(id + ' thead th').eq($(this).index()).text();
            html += '<th id="th'+$(this).index()+'"><input id="input'+$(this).index()+'" type="text" clas="form-control" placeholder="Search ' + title + '" /></th>';
          });
          $(id + ' thead tr').after('<tr role="row">'+html+'</tr>');
          var table = loadedDT.DataTable;
          // Apply the search
          table.columns().eq(0).each(function(colIdx) {
            $('#input'+colIdx).on('keyup change', function() {
              table
                .column(colIdx)
                .search(this.value)
                .draw();
            });
          });
        });

    });

    backToTop.init({
        theme: 'classic', // Available themes: 'classic', 'sky', 'slate'
        animation: 'fade' // Available animations: 'fade', 'slide'
    });
})(angular, backToTop);
